package com.feng.fengDUtil.dao;


import com.feng.fengDUtil.Reflections;
import com.feng.fengDUtil.dao.annotation.Geometry;
import com.feng.fengDUtil.dao.converter.Converter;

import java.lang.reflect.Field;

public class FieldInfo {
    private boolean id;
    private boolean idGenerateByDb;
    private Geometry geometry;
    private Field field;
    private String columnName;
    private Converter converter;

    public FieldInfo() {
    }

    public Converter getConverter() {
        return this.converter;
    }

    public void setConverter(Converter converter) {
        this.converter = converter;
    }

    public Object getWrapValue(Object bean) {
        Object value = Reflections.getFieldValue(bean, this.field);
        if (value == null) {
            return null;
        } else {
            return this.converter == null ? value : this.converter.wrap(value);
        }
    }

    public static FieldInfo buildFieldInfo(boolean id, Field field, String columnName) {
        FieldInfo fieldInfo = new FieldInfo();
        fieldInfo.setId(id);
        fieldInfo.setField(field);
        fieldInfo.setColumnName(columnName);
        return fieldInfo;
    }

    public static FieldInfo buildFieldInfo(boolean id,Field field, String columnName, boolean idGenerateByDb) {
        FieldInfo fieldInfo = new FieldInfo();
        fieldInfo.setId(id);
        fieldInfo.setField(field);
        fieldInfo.setColumnName(columnName);
        fieldInfo.setIdGenerateByDb(idGenerateByDb);
        return fieldInfo;
    }

    public static FieldInfo buildFieldInfo(boolean id, Field field, String columnName, boolean idGenerateByDb,Geometry geometry) {
        FieldInfo fieldInfo = new FieldInfo();
        fieldInfo.setId(id);
        fieldInfo.setField(field);
        fieldInfo.setColumnName(columnName);
        fieldInfo.setIdGenerateByDb(idGenerateByDb);
        fieldInfo.setGeometry(geometry);
        return fieldInfo;
    }

    public boolean isIdGenerateByDb() {
        return this.idGenerateByDb;
    }

    public void setIdGenerateByDb(boolean idGenerateByDb) {
        this.idGenerateByDb = idGenerateByDb;
    }

    public boolean isId() {
        return this.id;
    }

    public void setId(boolean id) {
        this.id = id;
    }

    public Geometry getGeometry() {
        return geometry;
    }

    public void setGeometry(Geometry geometry) {
        this.geometry = geometry;
    }

    public Field getField() {
        return this.field;
    }

    public void setField(Field field) {
        this.field = field;
    }

    public String getColumnName() {
        return this.columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public String toString() {
        return "{id = " + this.id + " , idGenerateByDb = " + this.idGenerateByDb + ",field =" + this.field.getName() + ",columnName =" + this.columnName + "} \n";
    }
}
